/***************************************** 
 * Desc: UGUI通用按钮，事件逻辑对其UGUIPanel，主要用来获取Button修改Unity对Button封装好的一系列效果
*****************************************/

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Common.UGUI.View {

    
	[RequireComponent(typeof(Button))]
	public class UGUIButton:UBase// : UGUIPanel 
    {
        public UGUIButton(GameObject go)
        {
            _button = go.GetComponent<Button>();
        }
        public const string TextGameObjectName = "Text"; 

        #region 域

        public Button Button {

            get {

                if(_button == null) {

                    _button = _gameObject.GetComponent<Button>();
                }

                return _button;
            }
        }

        /// <summary>
        /// 返回子对象的Text组件，注意有可能会是一个null值。
        /// </summary>
        public Text Text {

            get {

                if (_text == null) {

                    Transform textTransform = _transform.Find(TextGameObjectName);

                    if (textTransform != null) {

                        _text = textTransform.GetComponent<Text>();
                    }
                }

                return _text;
            }
        }

        private Button _button;
        private Text _text;

        #endregion

        #region 覆盖生命周期

        /// <summary>
        /// 子类如有需要覆盖，在Mono的Awake最后调用
        /// </summary>
        //protected override void OnAwake() {

        //    base.OnAwake();

        //    Image.raycastTarget = true;

        //    _button = GameObject.GetComponent<Button>();
        //    _button.onClick.AddListener(OnButtonClicked);

        //    Transform textTransform = Transform.Find(TextGameObjectName);

        //    if (textTransform != null) {
        //        _text = textTransform.GetComponent<Text>();
        //    }
        //}

        ///// <summary>
        ///// 子类如有需要覆盖，在Mono的OnDestroy最先调用
        ///// </summary>
        //protected override void OnDestroyByMono() {

        //    _button.onClick.RemoveAllListeners();
        //}

        #endregion

        #region 覆盖UGUIPanel回调函数

        //public override void OnPointerClick(PointerEventData eventData) {
        //    // 啥也不做，交给OnButtonClicked
        //}

        #endregion

        #region 接口

        //public override void SetIsInteractable(bool isInteractable) {

        //    //if (isInteractable) {

        //    //    _isInteractable = true;

        //    //    Image.raycastTarget = _isInteractable;

        //    //    Button.interactable = _isInteractable;
        //    //} else {

        //    //    _isInteractable = false;

        //    //    Image.raycastTarget = _isInteractable;

        //    //    Button.interactable = _isInteractable;
        //    //}
        //}

        #endregion

        #region 设置回调

        /// <summary>
        /// UGUIButton设置点击回调的便捷方法,等价于SetPointerClickCallback，不过回调的Action函数不用接收参数了，判断是否注册了也用IfHasPointerClickCallback
        /// </summary>
        public void SetClikcedCallback(string callbackName, Action callback, bool isReplace = false) {
            //SetPointerClickCallback(callbackName, 
            //    (pointerData) => { 
            //        if(callback != null) {
            //            callback();
            //        }
            //    }, isReplace);
        }

        /// <summary>
        /// UGUIButton移除点击回调的便捷方法
        /// </summary>
        public void RemoveClickedCallback(string callbackName) {
            //RemovePointerClickCallback(callbackName);
        }

        #endregion

        #region 回调转接

        private void OnButtonClicked() {

            // 直接调用基类的OnPointerClicked
           // base.OnPointerClick(null);
        }

        #endregion
    }
}
